package cn.rydiy.rmf.common;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.AttributeSet;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;

/**
 * Guide Display Class
 * 
 * @author Rydiy
 * @version 1.0.0
 * @Date 2012-07-30
 */
public class RmfGuide {
	private ArrayList<Integer> mGuideImgs; // guide images' id
	private RmfGallery mGallery; // guide gallery
	private Resources mRes; // Resources
	private Activity mActivity; // MainActivity
	private int mCurPos; // gallery current page

	/**
	 * Constructor
	 * 
	 * @param guideImgs
	 *            : A array of guide-images' id
	 * @param activity
	 *            : Application's main acitivity
	 * @param res
	 *            : A Resources instance for your application's package
	 */
	public RmfGuide(ArrayList<Integer> guideImgs, Activity activity,
			Resources res) {
		mGuideImgs = guideImgs;
		mActivity = activity;
		mRes = res;
		mCurPos = 0;
	}

	/**
	 * Show guide
	 */
	public void show() {
		// get layout
		LinearLayout layout = (LinearLayout) mActivity.findViewById(mRes
				.getIdentifier("rmf_id_splash_ly", "id",
						mActivity.getPackageName()));
		mGallery = new RmfGallery(mActivity);
		ImageAdapter imgAdapter = new ImageAdapter(mActivity);
		mGallery.setAdapter(imgAdapter);
		// add gallery to layout
		layout.removeAllViews();
		layout.addView(mGallery, new LayoutParams(
				LinearLayout.LayoutParams.FILL_PARENT,
				LinearLayout.LayoutParams.FILL_PARENT));
	}

	/**
	 * Custom gallery
	 * 
	 * @author Rydiy
	 * 
	 */
	private class RmfGallery extends Gallery {
		public RmfGallery(Context context) {
			super(context);
		}

		public RmfGallery(Context context, AttributeSet attrs) {
			super(context, attrs);
		}

		private boolean isScrollingLeft(MotionEvent e1, MotionEvent e2) {
			return e2.getX() > e1.getX();
		}

		@Override
		public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
				float velocityY) {
			int kEvent;
			if (isScrollingLeft(e1, e2)) {
				// Check if scrolling left
				kEvent = KeyEvent.KEYCODE_DPAD_LEFT;
			} else {
				// Otherwise scrolling right
				kEvent = KeyEvent.KEYCODE_DPAD_RIGHT;

				// if it is the last page, load main layout
				if (mCurPos == getSelectedItemPosition()
						&& mCurPos == getCount() - 1) {
					mActivity.setContentView(mRes.getIdentifier("main",
							"layout", mActivity.getPackageName()));
				} else {
					mCurPos = getSelectedItemPosition();
				}

			}

			onKeyDown(kEvent, null);
			return true;
		}

	}

	/**
	 * ImageAdapter for gallery
	 * 
	 * @author Rydiy
	 * 
	 */
	private class ImageAdapter extends BaseAdapter {
		private Context mContext = null;

		public ImageAdapter(Context c) {
			this.mContext = c;
		}

		public int getCount() {
			return mGuideImgs.size();
		}

		public Object getItem(int position) {
			return position;
		}

		public long getItemId(int position) {
			return position;
		}

		public View getView(int position, View convertView, ViewGroup parent) {
			ImageView mImageView = new ImageView(mContext);
			int id = mGuideImgs.get(position);
			Bitmap bmp = BitmapFactory.decodeResource(mRes, id);
			mImageView.setImageBitmap(bmp);
			mImageView.setScaleType(ImageView.ScaleType.FIT_XY);
			mImageView.setLayoutParams(new android.widget.Gallery.LayoutParams(
					android.widget.Gallery.LayoutParams.FILL_PARENT,
					android.widget.Gallery.LayoutParams.FILL_PARENT));
			return mImageView;
		}
	}
}
